package com.nebula.service.impl;

import com.nebula.context.BaseContext;
import com.nebula.dto.ShoppingCartDTO;
import com.nebula.entity.Dish;
import com.nebula.entity.Setmeal;
import com.nebula.entity.ShoppingCart;
import com.nebula.mapper.DishMapper;
import com.nebula.mapper.SetmealMapper;
import com.nebula.mapper.ShoppingCartMapper;
import com.nebula.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cart = shoppingCartMapper.list(shoppingCart);

        if(cart != null && cart.size() > 0){
            ShoppingCart cart0 = cart.get(0);
            cart0.setNumber(cart0.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart0);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            Long dishId = shoppingCart.getDishId();
            if(dishId != null){
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else {
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCartMapper.insert(shoppingCart);


        }
    }

    @Override
    public List<ShoppingCart> ShowShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> cart = shoppingCartMapper.list(shoppingCart);
        if(cart != null && cart.size() > 0){
            ShoppingCart cart0 = cart.get(0);
            Integer number = cart0.getNumber();
            if (number == 1){
                shoppingCartMapper.delete(cart0);
            }else {
                cart0.setNumber(number- 1);
                shoppingCartMapper.updateNumberById(cart0);
            }
        }
    }
}
